package com.maoye.ossp.common.web.taglib;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
/**
 *  *  
 * @author xiaohan
 * @date 2014年3月31日
 * @version 
 * @filename com.maoye.ossp.common.web.taglib.UpLoadTag.java
 */
public class UpLoadTag  extends TagSupport{
	
	Logger logger = Logger.getLogger(UpLoadTag.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = 8033078121407287559L;
	private String accept;    //容许上传文件类型
	private String maxFileSize; //最大上传文件数量
	private String prefixURL = "/sys/att";
	private int indexInThisPage;
	/**
	 * 处理的上传的URL
	 */
	private String handlerUploadURL = prefixURL + "/upload";
	/**
	 * 处理的下载的URL
	 */
	private String handlerDownloadURL = prefixURL + "/download";
	/**
	 * 下载列表
	 */
	private String downloadFileListURL = prefixURL + "/attr/download/list";
	/**
	 * 打开模式窗口
	 */
	private String dialogURL = prefixURL + "/upload/dialog";
	
	
	private String modleName;
	private String key;
	private String soureBillID;
	
	/**
	 * 
	 * @author xiaohan
	 * @data 2014年3月31日
	 */
	private void iniPath() {
		handlerUploadURL = prefixURL + "/upload";
	    handlerDownloadURL = prefixURL + "/download";
	    downloadFileListURL = prefixURL + "/attr/download/list";
	    dialogURL = prefixURL + "/upload/dialog";
	}

	public int doStartTag() {
		try {
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();

			if (!prefixURL.startsWith(request.getContextPath())) {
				prefixURL = request.getContextPath() + prefixURL;
				iniPath();
			}
			
			StringBuilder sb = new StringBuilder();
			if (indexInThisPage == 0) {
		        sb.append("<script type=\"text/javascript\">\r\n");
		        sb.append("\t(function($){\r\n");
		        sb.append("\t\t   $.fn.getAttachmentInfo = function(key){\r\n");
		        sb.append("\t\t\t   var obj = $('[name=\"key\"][value=\"'+key+'\"]').parent();\r\n");
		        sb.append("\t\t\t   return obj.find('[name=\"key\"]').data('ossp-attachment-contr');\r\n");
		        sb.append("\t\t   }\r\n");
		        sb.append("\t})(jQuery);\r\n");
		        sb.append("\tfunction Att_OpenDialog(th){\r\n");
		        sb.append("\t\tvar obj = cacheAtta = $(th).parent();\r\n");
		        sb.append("\t\tvar dialogURL = obj.find('[name=\"dialogURL\"]').val();\r\n");
		        sb.append("\t\tvar r=Math.random();\r\n");
		        sb.append("\t\tobj.find('[name=\"random\"]').val(r);\r\n");
		        sb.append("\t\t\r\n");
		        sb.append("\t\tvar dialogSty = 'dialogWidth=800px;dialogHeight=500px;center=yes;resizable=no;status=no;';\r\n");
		        sb.append("\t\tvReturnValue = window.showModalDialog(dialogURL+\"?r=\"+r,window,dialogSty);\r\n");
		        sb.append("\t\tif(vReturnValue){\r\n");
		        sb.append("\t\t\tvar ids = new Array();\r\n");
		        sb.append("\t\t\tfor (var i=0, file; file=vReturnValue.files[i]; i++){\r\n");
		        sb.append("\t\t\t\tids[i]=file.attid;\r\n");
		        sb.append("\t    \t}\r\n");
		        sb.append("\t\t\tvReturnValue.o = {};\r\n");
		        sb.append("\t\t\tvReturnValue.o.formatFileSize = function(size){\r\n");
		        sb.append("    \t\t\treturn (size / 1000)+'KB';\r\n");
		        sb.append("    \t\t};\r\n");
		        sb.append("    \t\tvReturnValue.o.formatFileIcon = function(fileType){\r\n");
		        sb.append("\t\t\t\tswitch(fileType){\r\n");
		        sb.append("\t\t\t\t\tcase \"application/msword\":return \"doc_1.png\";\r\n");
		        sb.append("\t\t\t\t\tcase \"application/vnd.openxmlformats-officedocument.wordprocessingml.document\":return \"doc_1.png\";\r\n");
		        sb.append("\t\t\t\t\tcase \"application/vnd.ms-excel\":return \"xls_1.png\";\r\n");
		        sb.append("\t\t\t\t\tcase \"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\":return \"xls_1.png\";\r\n");
		        sb.append("\t\t\t\t\tcase \"application/vnd.ms-powerpoint\":return \"pdf_1.png\";\r\n");
		        sb.append("\t\t\t\t\tcase \"application/pdf\":return \"pdf_1.png\";\r\n");
		        sb.append("\t\t\t\t\tcase \"text/plain\":return \"text_1.png\";\r\n");
		        sb.append("\t\t\t\t\tcase \"application/x-jpg\":return \"img_1.png\";\r\n");
		        sb.append("\t\t\t\t\tcase \"application/x-ico\":return \"img_1.png\";\r\n");
		        sb.append("\t\t\t\t\tcase \"application/x-bmp\":return \"img_1.png\";\r\n");
		        sb.append("\t\t\t\t\tcase \"image/gif\":return \"img_1.png\";\r\n");
		        sb.append("\t\t\t\t\tcase \"image/png\":return \"img_1.png\";\r\n");
		        sb.append("\t\t\t\t\tcase \"image/jpeg\":return \"img_1.png\";\r\n");
		        sb.append("\t\t\t\t\tdefault:return \"documents_1.png\";\r\n");
		        sb.append("\t\t\t\t}\r\n");
		        sb.append("    \t\t};\r\n");
		        sb.append("\t\t\tvar html = template('file-view-tmp', vReturnValue);\r\n");
		        sb.append("\t\t\t$(\"#file-view-"+key+"\").html(html);\r\n");
		        sb.append("\t\t\tobj.find('[name=\"key\"]').data('ossp-attachment-contr',ids);\r\n");
		        sb.append("\t\t}\r\n");
		        sb.append("\t}\r\n");
		        sb.append("\tfunction getVal(id,random){\r\n");
		        sb.append("\t\tvar obj = $('[name=random][value=\"'+random+'\"]').parent();//$('#ossp-attachment-contr');\r\n");
		        sb.append("\t\treturn obj.find('[name=\"'+id+'\"]').val();\r\n");
		        sb.append("\t}\r\n");
		        sb.append("</script>\r\n");
		        sb.append("<script id=\"file-view-tmp\" type=\"text/html\">\r\n");
		        sb.append("\t{{each files as file}}\r\n");
		        sb.append("\t\t  <tr>\r\n");
		        sb.append("               <td><span class=\"preview img_preview\"><img src=\"");
		        sb.append(request.getContextPath()+"/static");
		        sb.append("/images/icon/fileIcon/{{o.formatFileIcon(file.fileType)}}\" height=\"26\" width=\"26\"></span></td>\r\n");
		        sb.append("               <td><p class=\"name\">{{file.name}}</p></td>\r\n");
		        sb.append("               <td><p class=\"size\">{{o.formatFileSize(file.size)}}</p> </td>\r\n");
		        sb.append("               <td> \r\n");
		        sb.append("                </td>\r\n");
		        sb.append("           </tr>\r\n");
		        sb.append("\t{{/each}}\r\n");
		        sb.append("</script>\r\n");
			}
	        sb.append("<div class=\"bs-example\" name=\"ossp-attachment-contr\">\r\n");
	        sb.append("\t<input type=\"hidden\" name=\"handlerUploadURL\" value=\""+handlerUploadURL+"\">\r\n");
	        sb.append("\t<input type=\"hidden\" name=\"handlerDownloadURL\" value=\""+handlerDownloadURL+"\">\r\n");
	        sb.append("\t<input type=\"hidden\" name=\"downloadFileListURL\" value=\""+downloadFileListURL+"\">\r\n");
	        sb.append("\t<input type=\"hidden\" name=\"dialogURL\" value=\""+dialogURL+"\">\r\n");
	        sb.append("\t<input type=\"hidden\" name=\"modleName\" value=\""+modleName+"\">\r\n");
	        sb.append("\t<input type=\"hidden\" name=\"key\" value=\""+key+"\">\r\n");
	        sb.append("\t<input type=\"hidden\" name=\"soureBillID\" value=\""+StringUtils.trimToEmpty(soureBillID)+"\">\r\n");
	        sb.append("\t<input type=\"hidden\" name=\"accept\" value=\""+StringUtils.trimToEmpty(accept)+"\">\r\n");
	        sb.append("\t<input type=\"hidden\" name=\"maxFileSize\" value=\""+StringUtils.trimToEmpty(maxFileSize)+"\">\r\n");
	        sb.append("\t<input type=\"hidden\" name=\"random\" value=\"\">\r\n");
	        sb.append("\t<button type=\"button\" class=\"btn btn-info fileinput-button img_padding_btn img_btn_info btn_3\" onclick=\"Att_OpenDialog(this)\">上传</button>\r\n");
	        sb.append("    <table role=\"presentation\" class=\"img_table padding\">\r\n");
	        sb.append("       <tbody id=\"file-view-"+key+"\">\r\n");
	        sb.append("       </tbody>\r\n");
	        sb.append("   </table>\r\n");
	        sb.append("</div>\r\n");
	        
			pageContext.getOut().print(sb.toString());
		}catch(Exception e){
			logger.debug(e);
		}
		return EVAL_BODY_INCLUDE;
		
	}
	
	public String getMaxFileSize() {
		return maxFileSize;
	}

	public void setMaxFileSize(String maxFileSize) {
		this.maxFileSize = maxFileSize;
	}

	public String getAccept() {
		return accept;
	}

	public void setAccept(String accept) {
		this.accept = accept;
	}

	public String getModleName() {
		return modleName;
	}


	public void setModleName(String modleName) {
		this.modleName = modleName;
	}


	public String getKey() {
		return key;
	}


	public void setKey(String key) {
		this.key = key;
	}


	public String getSoureBillID() {
		return soureBillID;
	}


	public void setSoureBillID(String soureBillID) {
		this.soureBillID = soureBillID;
	}


	public String getHandlerUploadURL() {
		return handlerUploadURL;
	}


	public void setHandlerUploadURL(String handlerUploadURL) {
		if (StringUtils.isBlank(handlerUploadURL)) {
			return;
		}
		this.handlerUploadURL = handlerUploadURL;
	}


	public String getHandlerDownloadURL() {
		return handlerDownloadURL;
	}


	public void setHandlerDownloadURL(String handlerDownloadURL) {
		if (StringUtils.isBlank(handlerDownloadURL)) {
			return;
		}
		this.handlerDownloadURL = handlerDownloadURL;
	}


	public String getDownloadFileListURL() {
		return downloadFileListURL;
	}


	public void setDownloadFileListURL(String downloadFileListURL) {
		if (StringUtils.isBlank(downloadFileListURL)) {
			return;
		}
		this.downloadFileListURL = downloadFileListURL;
	}


	/**
	 * @return the dialogURL
	 */
	public String getDialogURL() {
		return dialogURL;
	}


	/**
	 * @param dialogURL the dialogURL to set
	 */
	public void setDialogURL(String dialogURL) {
		if (StringUtils.isBlank(dialogURL)) {
			return;
		}
		this.dialogURL = dialogURL;
	}

	public int getIndexInThisPage() {
		return indexInThisPage;
	}

	public void setIndexInThisPage(int indexInThisPage) {
		this.indexInThisPage = indexInThisPage;
	}


	
}
